﻿namespace Graphix.Charts.Base
{
    using System.Collections.Generic;
    using System.Linq;

    using Graphix.Elements;

    public abstract class ColumnBase : BothAxisAndLegend<float>
    {
        protected List<ValueSet<float>> BarSet { get; set; }

        public ColumnBase()
        {
            this.BarSet = new List<ValueSet<float>>();
        }

        public bool IsStacked { get; set; }

        public void AddSets(List<string> sets)
        {
            foreach (string s in sets)
            {
                this.SetsName.Add(s);
            }
        }

        public override void AddSeries(string barName, List<float> values)
        {
            while (this.SetsName.Count < values.Count)
            {
                this.SetsName.Add(string.Empty);
            }

            this.BarSet.Add(new ValueSet<float>(this.GetNextColor(), barName, values));
        }

        public virtual void AddSeries(string barset, string barName, List<float> values)
        {
            if (!this.SetsName.Any(x => x == barset))
            {
                this.SetsName.Add(barset);
            }

            this.BarSet.Add(new ValueSet<float>(this.GetNextColor(), barName, values));
        }

        protected override void PreDraw()
        {
            float barMin = this.BarSet.Min(x => x.Values.Min());
            float barMax;
            if (this.IsStacked)
            {
                barMax = this.BarSet.Sum(x => x.Values.Max());
            }
            else
            {
                barMax = this.BarSet.Max(x => x.Values.Max());
            }

            if (this.XAxis.Minimum != 0)
            {
                if (this.XAxis.Minimum > barMin || float.IsNaN(this.XAxis.Minimum))
                {
                    this.XAxis.Minimum = barMin;
                }
            }

            if (this.XAxis.Maximum == 0 ||
                this.XAxis.Maximum < this.XAxis.Minimum ||
                this.XAxis.Maximum < barMax ||
                float.IsNaN(this.XAxis.Maximum))
            {
                this.XAxis.Maximum = barMax;
            }

            this.YAxis.Minimum = this.XAxis.Minimum;
            this.YAxis.Maximum = this.XAxis.Maximum;
        }

        protected override void DrawChart()
        {
            if (this.DisplayLegend)
            {
                this.BuildLegend(this.BarSet.Select(x => x.Name).ToList(), this.BarSet);
            }

            this.DrawTitle();
            this.AddYAxisTitle();
            this.AddXAxisTitle();

            this.AddTicks();

            this.AddBars();
            ////this.AddElement(new Line(this.border.Left, this.border.Top + this.border.Height / 2,
            ////    this.border.Left + this.border.Width, this.border.Top + this.border.Height / 2, Color.Black));

            //this.AddElement(new Line(this.border.Width / 2 + this.border.Left, this.border.Top,
            //    this.border.Width / 2 + this.border.Left, this.border.Top + this.border.Height,
            //    Color.Black));
        }

        protected abstract void AddTicks();

        protected abstract void AddBars();
    }
}
